<?php

namespace Xin\EasyQueue;

use ReflectionClass;
use Xin\Support\Framework;

trait Dispatchable
{
    /**
     * Dispatch the job with the given arguments.
     *
     * @return PendingDispatch
     */
    public static function dispatch(...$data)
    {
        if (Framework::isHyperf()) {
            $job = new static(...$data);
            return (new PendingDispatch($job, $job->queue, $job->connection));
        } elseif (Framework::isWebman()) {
            $reflection = new ReflectionClass(static::class);
            $properties = $reflection->getDefaultProperties();
            $queue = $properties['queue'] ?? Queue::getDefaultQueueName();
            $connection = $properties['connection'] ?? Queue::getDefaultConnectionName();

            return (new PendingDispatch($data[0] ?? null, $queue, $connection));
        } else {
            Framework::throwUnknownFrameworkException();
        }
    }
}
